package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.dao.MenuDao;
import com.qf.dao.RoleDao;
import com.qf.dao.UserDao;
import com.qf.pojo.Menu;
import com.qf.pojo.Role;
import com.qf.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 实现SpringSecurity的接口, 定义登录业务逻辑
 * SpringSecurity会调用这个接口
 * @author 千锋健哥
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    //从配置文件中获取密码
//    @Value("${spring.security.user.password}")
//    private String pwd;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

//    @Override
//    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
//        if (userName == null) {
//            throw new UsernameNotFoundException("用户名错误!");
//        }
//
//        //创建加密对象
//        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
//        //对配置文件中的密码进行加密
//        String encode = bCryptPasswordEncoder.encode(pwd);
//        //第一个参数: 用户名, 第二个参数: 密码, 第三个参数:用户拥有的权限集合
//        return new User(userName, encode, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,pm"));
//    }

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        if (userName == null) {
            throw new UsernameNotFoundException("用户名错误!");
        }

        //从数据库表中查询用户
        //创建查询条件对象
        QueryWrapper<Users> queryWapper = new QueryWrapper<>();
        //设置查询条件, 根据用户名精确查询
        queryWapper.eq("name", userName);
        //查询一条数据
        Users users = userDao.selectOne(queryWapper);
        if (users == null) {
            throw new UsernameNotFoundException("用户不存在!");
        }

        //创建权限集合对象
        List<GrantedAuthority> grantedList = new ArrayList<>();

        //获取用户角色集合
        List<Role> roleList = roleDao.findRoleByUserId(users.getId());
        if (roleList != null) {
            for (Role role : roleList) {
                SimpleGrantedAuthority auth = new SimpleGrantedAuthority(role.getName());
                grantedList.add(auth);
            }
        }

        //获取用户权限菜单集合
        List<Menu> menuList = menuDao.findMenuByUserId(users.getId());
        if (menuList != null) {
            for (Menu menu : menuList) {
                SimpleGrantedAuthority auth = new SimpleGrantedAuthority(menu.getPerms());
                grantedList.add(auth);
            }
        }


        //给明文密码进行BCrypt算法加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(users.getPassword());

        //第一个参数: 用户名, 第二个参数: 密码, 第三个参数:用户拥有的权限集合
        return new User(userName, encode, grantedList);
    }

}
